﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.SpecificServiceType;

namespace BCData.TMS.SpecificServiceType
{
    public class SpecificServiceTypeData : ISpecificServiceTypeData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    SpecificServiceTypeId,
		    SpecificServiceType,
		    ServiceTypeName,
		    HasRemark,
		    ServiceRemark,
		    Enabled,
		    CreateAdminId,
		    CreateAdminName,
		    CreateTime,
		    UpdateTime,
		    EditAdminId,
		    EditAdminName
        FROM
            specificservicetype";



        #region 构造函数
        public SpecificServiceTypeData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        public async Task<SpecificServiceTypeEntity> InsertAsync(SpecificServiceTypeEntity entity)
        {
            return await this.database.Master.InsertAsync(entity);
        }
        #endregion

        #region updateAsync
        public async Task<SpecificServiceTypeEntity> UpdateAsync(SpecificServiceTypeEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity);
        }
        #endregion 

        #region Delete
        public async Task<bool> DeleteAsync(int specificServiceTypeId)
        {
            string sql = "DELETE FROM specificservicetype WHERE SpecificServiceTypeId = ?specificServiceTypeId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SpecificServiceTypeId", specificServiceTypeId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        public async Task<SpecificServiceTypeEntity> GetEntityAsync(int specificServiceTypeId)
        {
            string sql = QUERY + " WHERE  SpecificServiceTypeId = ?specificServiceTypeId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SpecificServiceTypeId", specificServiceTypeId, DbType.Int64);
            return await this.database.Master.QueryFirstOrDefaultAsync<SpecificServiceTypeEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询特殊服务类型记录
        public async Task<(IEnumerable<SpecificServiceTypeEntity>, int)> GetListAsync(bool? isEnabled,int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();
            if(isEnabled.HasValue)
            {
                wheres.Add("Enabled=?Enabled");
                parameters.Add("?Enabled", isEnabled, DbType.Boolean);
            }

            string sql = string.Format(@"{0} {1} LIMIT {2},{3} ; SELECT COUNT(1) FROM  specificservicetype  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize
                  );

            return await this.database.QueryMultipleAsync<SpecificServiceTypeEntity, int>(sql, parameters);
        }
        #endregion

        #region 获取全部记录
        public async Task<IEnumerable<SpecificServiceTypeEntity>> GetOnlyListAsync(bool? isEnabled)
        {
            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();
            if (isEnabled.HasValue)
            {
                wheres.Add("Enabled=?Enabled");
                parameters.Add("?Enabled", isEnabled, DbType.Boolean);
            }
            string sql = string.Format(@"{0} {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "");


            return await database.Slave.QueryAsync<SpecificServiceTypeEntity>(sql);
        }
        #endregion
    }
}
//接口



